﻿Imports CRM.BusinessLayer
Imports System.ComponentModel

Partial Public Class frmAddAddress

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private _AddressType As People.AddressTypes
    Private _AddressLinkId As Long
    Private _AddressId As Long

    Public Property AddressType() As People.AddressTypes
        Get
            Return _AddressType
        End Get
        Set(ByVal value As People.AddressTypes)
            _AddressType = value
        End Set
    End Property

    Private EditAddress As People.AddressInfo

    Public Property AddressLinkId() As Long
        Get
            Return _AddressLinkId
        End Get
        Set(ByVal value As Long)
            _AddressLinkId = value

            Dim myAddress As People.AddressInfo = People.GetAddress(value)

            If Not IsNothing(myAddress) Then
                Me.EditAddress = myAddress

            End If

            _AddressId = People.GetAddressIdFromAddressLink(value)


        End Set
    End Property

    Private Sub frmAddAddress_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
        If e.Key = Key.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub AddAddress_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        If Me.AddressType = People.AddressTypes.NotSet Then
            CommonFuncs.DisplayMessage("Address Type Not Set", CommonDialog.CustomDialogIcons.Information, "Info")
            Me.Close()
        End If

        ' populate drop down lists
        Me.Address1.CountryComboBox.DisplayMemberPath = "country_name"
        Me.Address1.CountryComboBox.SelectedValuePath = "country_id"
        Me.Address1.CountryComboBox.ItemsSource = CType(CommonDataFunctions.CountryList, IListSource).GetList

        Me.Address1.CountryComboBox.SelectedIndex = -1

        Me.Address1.CountyComboBox.DisplayMemberPath = "county_name"
        Me.Address1.CountyComboBox.SelectedValuePath = "county_id"
        Me.Address1.CountyComboBox.ItemsSource = CType(CommonDataFunctions.CountyList, IListSource).GetList
        Me.Address1.CountyComboBox.SelectedIndex = -1


        If Not IsNothing(Me.EditAddress) Then

            With Me.EditAddress
                Me.Address1.Description.Text = .AddressName
                Me.Address1.AddressText.Text = .AddressText
                Me.Address1.PostcodeText.Text = .Postcode
                If .CountryName = String.Empty Then
                    Me.Address1.CountryComboBox.SelectedIndex = -1
                Else
                    Me.Address1.CountryComboBox.Text = .CountryName
                End If

                If .CountryId = GlobalCRM.COUNTRY_UK Then
                    Me.Address1.CountyComboBox.Visibility = Windows.Visibility.Visible
                    Me.Address1.RegionText.Visibility = Windows.Visibility.Hidden
                    Me.Address1.CountyComboBox.Text = .Region
                Else
                    Me.Address1.CountyComboBox.Visibility = Windows.Visibility.Hidden
                    Me.Address1.RegionText.Visibility = Windows.Visibility.Visible
                    Me.Address1.RegionText.Text = .Region
                End If

            End With

        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim Region As String
        Dim LinkId As Long

        If Me.Address1.Description.Text.Trim = String.Empty Then
            CommonFuncs.DisplayMessage("You must give the address a name", CommonDialog.CustomDialogIcons.Information, "Info")
            Exit Sub
        End If

        If Me.Address1.AddressText.Text.Trim = String.Empty Then
            CommonFuncs.DisplayMessage("Address box cannot be left blank.", CommonDialog.CustomDialogIcons.Information, "Info")
            Exit Sub
        End If

        If Me.Address1.CountryComboBox.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("You must select a country!", CommonDialog.CustomDialogIcons.Information, "Info")
            Exit Sub
        End If

        If Me.Address1.CountyComboBox.Visibility = Windows.Visibility.Visible Then
            Region = Me.Address1.CountyComboBox.Text
        Else
            Region = Me.Address1.RegionText.Text
        End If

        If Me.AddressType = People.AddressTypes.Company Then
            LinkId = GlobalCRM.CurrentCompanyID
        ElseIf Me.AddressType = People.AddressTypes.Person Then
            LinkId = GlobalCRM.CurrentCandidateID
        End If


        If Me.AddressLinkId = 0 Then
            'New record
            If People.AddAddress(Me.Address1.AddressText.Text, Region, Me.Address1.CountryComboBox.Text, Me.Address1.PostcodeText.Text, GlobalCRM.CurrentConName, Me.AddressType, Me.Address1.Description.Text, LinkId) > 0 Then
                Me.Close()
            End If

        Else
            'Edit record
            If People.AddAddress(Me.Address1.AddressText.Text, Region, Me.Address1.CountryComboBox.Text, Me.Address1.PostcodeText.Text, GlobalCRM.CurrentConName, Me.AddressType, Me.Address1.Description.Text, 0, Me.AddressLinkId, Me._AddressId, Me.EditAddress.AddressName, Me.EditAddress.AddressText, Me.EditAddress.Postcode, Me.EditAddress.CountryName) > 0 Then
                Me.Close()
            End If

        End If

    End Sub
End Class